#include "SoundManager.h" 

#include "cocos2d.h"
USING_NS_CC;

static CSoundManager s_SharedCSoundManager;



CSoundManager::CSoundManager(){
    
      m_isPlayEffect=true;
      m_isPlayBackGround=true;
     
}

CSoundManager* CSoundManager::sharedSoundManager(void)
{
    static bool s_bFirstUseGameManager = true;
    if (s_bFirstUseGameManager)
    {
        s_bFirstUseGameManager = false; 
    }
    
    return &s_SharedCSoundManager;
}


void CSoundManager::loadConf(){
     
    m_isPlayBackGround=CCUserDefault::sharedUserDefault()->getBoolForKey("setting_backgroud_play",true);
    m_isPlayEffect=CCUserDefault::sharedUserDefault()->getBoolForKey("setting_effect_play",true);
 
    
    setBackGroundV(CCUserDefault::sharedUserDefault()->getIntegerForKey("setting_backgroud_volume",50));
    setEffectV(CCUserDefault::sharedUserDefault()->getIntegerForKey("setting_effect_volume",50));
}

void CSoundManager::saveConf(){
  
    CCUserDefault::sharedUserDefault()->setBoolForKey("setting_effect_play", m_isPlayEffect);
    CCUserDefault::sharedUserDefault()->setBoolForKey("setting_backgroud_play", m_isPlayBackGround);
    
    CCUserDefault::sharedUserDefault()->setIntegerForKey("setting_effect_volume", getEffectV());
    CCUserDefault::sharedUserDefault()->setIntegerForKey("setting_background_volume", getBackGroundV());
    
    
    
    CCUserDefault::sharedUserDefault()->flush();

}


void CSoundManager::setIsPlayEffect(bool value){
    m_isPlayEffect=value;
    
}
void CSoundManager::setIsPlayBackground(bool value){
    if(m_isPlayBackGround==value){
        return;
    }
    m_isPlayBackGround=value;
    
    if(m_isPlayBackGround){
        playBackground(backGroundPath.c_str());
    }else{
        stopBackground();
    }
}



void CSoundManager::stopBackground(){
    CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
}


void CSoundManager::setEffectV(int num){
    CocosDenshion::SimpleAudioEngine::sharedEngine()->setEffectsVolume(num/100.0f);
}


void CSoundManager::setBackGroundV(int num){
    CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(num/100.0f);
}



int CSoundManager::getEffectV(){
    return  CocosDenshion::SimpleAudioEngine::sharedEngine()->getEffectsVolume()*100;
}

int CSoundManager::getBackGroundV(){
    return  CocosDenshion::SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()*100;
} 

void CSoundManager::preloadBackground(const char* cUrl){
  
    const char* newUrl= CCFileUtils::sharedFileUtils()->fullResourcePath(cUrl).c_str(); 
    CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(newUrl);
    
}
void CSoundManager::preloadEffect(const char* cUrl ){
    string path = CCFileUtils::sharedFileUtils()->fullResourcePath(cUrl);
    CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(path.c_str());
}

void CSoundManager::playBtnSound(){
  //  playEffect("sound/click_btn.mp3");
}


void CSoundManager::playBackground(const char* cUrl){
    backGroundPath=cUrl;
    if(!m_isPlayBackGround){
        return;
    } 
    const char* newUrl= CCFileUtils::sharedFileUtils()->fullResourcePath(cUrl).c_str(); 
    CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(newUrl,true);
}



void CSoundManager::playEffect(const char* cUrl){
    if(!m_isPlayEffect ){
        return;
    } 
    const char* newUrl= CCFileUtils::sharedFileUtils()->fullPathForFilename(cUrl).c_str(); 
    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(newUrl,false);
}